package com.longge;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.stream.Collectors;

/**
 * @创建人 xinglongge
 * @创建时间 2021/7/14
 * @描述
 */
public class KSmallestPairs {

    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        List<List<Integer>> res = new ArrayList<>();
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> (o[0] + o[1])));
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                queue.add(new int[]{nums1[i], nums2[j]});
            }
        }
        for (int i = 0; i < k; i++) {
            int[] arr = queue.poll();
            if (Objects.nonNull(arr)) res.add(Arrays.asList(arr[0], arr[1]));
        }
        return res;
    }

    public static void main(String[] args) {
        new KSmallestPairs().kSmallestPairs(new int[]{1, 2, 4, 5, 6}, new int[]{3, 5, 7, 9}, 3);
    }
}
